package com.itheima.infoManager.arraylistDemo.manager.dao;

import com.itheima.infoManager.arraylistDemo.manager.domain.Teacher;

public class TeacherDao extends BaseTeacherDao{
    private static Teacher[] teachers = new Teacher[5];

    static {
        Teacher teacher1 = new Teacher("heima001","张三","23","1998-88-99");
        Teacher teacher2 = new Teacher("heima002","李四","24","2000-88-99");

        teachers[0]=teacher1;
        teachers[1]=teacher2;
    }

    //添加
    public boolean addTeacher(Teacher teacher) {
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher1 = teachers[i];
            if (teacher1==null){ //有数组索引处为空的地方
                teachers[i]=teacher; //就可以插入要添加的对象
                return true; //向上层返回true
            }
        }
        return false;//否则返回false
    }

    //返回数组——查看
    public Teacher[] findAllTeacher() {
        return teachers;
    }

    //删除
    public boolean deleteTeacher(String delId) {
        //调用getIndex方法，取得当前id在数组的索引位置
        int index = getIndex(delId);
        if (index==-1){
            return false;
        }else {
            teachers[index]=null;
            return true;
        }
    }

    //根据id查找其学生对象所在数组索引处
    public int getIndex(String id) {
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = TeacherDao.teachers[i];
            if (teacher!=null&&teacher.getId().equals(id)){
                return i;
            }
        }
        return -1;
    }

    //修改
    public boolean updateTeacher(String upId, Teacher teachers1) {
        //根据id查找其学生对象所在数组索引处
        int index = getIndex(upId);
        if (index==-1){
            return false;
        }else{
            teachers[index]=teachers1;
            return true;
        }

    }
}
